package com.study.zzedu.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.study.zzedu.R;
import com.study.zzedu.entity.Msg;

import java.util.List;

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.MViewHolder> {

    private List<Msg> m;
    public MsgAdapter(List<Msg> mList) {
        this.m = mList;
    }

    public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_layout,parent,false);
        MViewHolder holder = new MViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MViewHolder holder, int position) {
        if(m.size()<1)
            return;
        Msg mMsg = m.get(position);

        if(mMsg.getType() == Msg.TYPE_RECEIVED)
        {
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.rightLayout.setVisibility(View.GONE);
            holder.leftMsg.setText(mMsg.getContent());
            holder.leftName.setText(mMsg.getName());
        }else if(mMsg.getType() == Msg.TYPE_SENT)
        {
            holder.leftLayout.setVisibility(View.GONE);
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.rightMsg.setText(mMsg.getContent());
            holder.rightName.setText(mMsg.getName());
        }
    }

    @Override
    public int getItemCount() {
        return m.size();
    }

    public class MViewHolder extends RecyclerView.ViewHolder{
        LinearLayout leftLayout;
        LinearLayout rightLayout;
        TextView leftMsg;
        TextView rightMsg;
        TextView leftName;
        TextView rightName;
        public MViewHolder(View mView) {
            super(mView);
            leftLayout = (LinearLayout) mView.findViewById(R.id.left_layout);
            rightLayout = (LinearLayout) mView.findViewById(R.id.right_layout);
            leftMsg = (TextView) mView.findViewById(R.id.text_viewleft1);
            rightMsg = (TextView) mView.findViewById(R.id.text_viewright1);

            leftName = (TextView) mView.findViewById(R.id.text_left_name);
            rightName = (TextView) mView.findViewById(R.id.text_right_name);
        }
    }
}
